package com.gree.flowable.app.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gree.flowable.app.mapper.FormInfoMapper;
import com.gree.flowable.app.mapper.FormModelMapper;
import com.gree.flowable.app.pojo.form.FormInfo;
import com.gree.flowable.app.pojo.form.FormModel;
import com.gree.flowable.app.services.IFormExportExcelService;
import com.gree.flowable.app.tools.utils.Custemhandler;
import com.gree.flowable.app.tools.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author ：180849
 * @date ：Created in 2021/5/10 16:39
 * @description：导出FormExcel
 * @modified By：
 * @version: 1.0
 */
@Controller
@RequestMapping("/rest")
public class IFormExportExcelApi {
    @Autowired
    private IFormExportExcelService iFormExportExcelService;

    @Autowired
    private FormModelMapper formModelMapper;

    @Autowired
    private FormInfoMapper formInfoMapper;

    @GetMapping("/test")
    public String test(){
        return "test";
    }

    @RequestMapping(value = "/exportExcel",method = RequestMethod.POST)
    public ResponseEntity<byte[]> exportExcel(@RequestParam("formId")String formId,@RequestParam("processKey")String processKey) throws Exception {
        return iFormExportExcelService.exportFormExcel(formId, processKey);
    }


    @GetMapping("/excel/export/{formId}")
    public void download(HttpServletResponse response, @PathVariable("formId")String formId) throws IOException {
        QueryWrapper<FormInfo> formInfoQueryWrapper = new QueryWrapper<>();
        formInfoQueryWrapper.lambda().eq(FormInfo::getFormId,formId);
        FormInfo formInfo = formInfoMapper.selectOne(formInfoQueryWrapper);
        String formName = formInfo.getFormName();

        QueryWrapper<FormModel> fmQueryWrapper = new QueryWrapper<>();
        fmQueryWrapper.lambda().eq(FormModel::getFormId,formId);
        List<FormModel> formModelList = formModelMapper.selectList(fmQueryWrapper);
        List<List<String>> heardList = new LinkedList<>();
        List<String> formTheme =new LinkedList<>();
        formTheme.add("单据主题");
        heardList.add(formTheme);
        for (FormModel formModel :formModelList) {
//            Map<String,String> heardMap = new HashMap<>();
            List<String> heardList2 = new LinkedList<>();
            String label = formModel.getLabel();
            heardList2.add(label);
            heardList.add(heardList2);
        }

        try {
            EasyExcel.write(ExcelUtils.getOutputStream(formName,response, ExcelTypeEnum.XLSX))
                    .head(heardList).sheet(formName).registerWriteHandler(new Custemhandler()).registerWriteHandler(ExcelUtils.setMyCellStyle()).
                    doWrite(null);
        } catch (IOException e) {
            // 重置response
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            Map<String, String> map = new HashMap<String, String>();
            map.put("status", "failure");
            map.put("message", "下载文件失败" + e.getMessage());
            response.getWriter().println(JSON.toJSONString(map));
        }
        System.out.println(heardList);
    }
}
